package com.li.client.management;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.li.client.entities.User;
import com.li.client.service.DataServiceFactory;
import com.li.client.service.DataUtilityManager;
import com.li.client.util.AppStyle;
import com.li.client.util.StringUtilGwt;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class LoginManager {
	private Window window;
	private VLayout layout;
	private DynamicForm form;
	private Button login;
	private Button reset;
	private Executor executor;
	
	private TextItem userIdText;
	PasswordItem pwdItem;
	
	public LoginManager(Executor executor) {
		this.executor = executor;
		init();
		
	}
	
	private void init() {
		form = createDefaultForm();
		
		userIdText = new TextItem("userIdField", "User");
		pwdItem = new PasswordItem("pwdField", "Password");
		form.setFields(userIdText, pwdItem);
		
		login = new Button("Login");
		reset = new Button("Reset");
		
		HLayout btnLayout = new HLayout();
		btnLayout.addMember(login);
		
		btnLayout.addMember(reset);
		
		addBtnHandler();
		
		layout = new VLayout();
		layout.addMember(form);
		layout.addMember(btnLayout);
		
		
		
		initWindow();
		window.addItem(layout);
	}
	
	private void addBtnHandler()
	{
		login.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				if (userIdText.getValue() == null || pwdItem.getValue() == null) {
					SC.say("please input user id and password to login");
					return;
				} 
				
				String loginId = userIdText.getValueAsString().trim();
				String pwd = pwdItem.getValueAsString().trim();
				
				if (!StringUtilGwt.isNotBlank(loginId) || !StringUtilGwt.isNotBlank(pwd)) {
					SC.say("please input user id and password to login");
					return;
				} else {
					DataServiceFactory.getService().loginSystem(loginId, pwd, new AsyncCallback<User>() {
						
						@Override
						public void onSuccess(User result) {
							if (result == null) {
								SC.say("login name/password is not correct");
							} else {
								DataUtilityManager.setCurrentUser(result);
								executor.execute();
								window.destroy();
							}
						}
						
						@Override
						public void onFailure(Throwable caught) {
							SC.say("login fails");
							
						}
					});
					
				}
				
			}
		});
		
		reset.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				form.reset();
			}
		});
	}
	
	private DynamicForm createDefaultForm()
	{
		DynamicForm formLocal = new DynamicForm();
		formLocal.setWidth(AppStyle.px90);
		formLocal.setHeight(AppStyle.px50);
		formLocal.setPadding(AppStyle.px3);
		formLocal.setNumCols(AppStyle.px3);
		formLocal.setCanSelectText(Boolean.TRUE);
//		formLocal.setBorder("2px solid black");
		return formLocal;
	}
	
	
	private void initWindow() {
		window = new Window();
		window.setIsModal(Boolean.TRUE);
		window.setTitle("Login in system");
		window.setWidth(AppStyle.px300);
		window.setHeight(AppStyle.px120);
		
		window.addItem(layout);
		window.centerInPage();
	}
	
	public void show() {
		window.show();
	}
	
	
	
}
